var express = require("express");
var router = express.Router();
const db = require("../db");
const querystring = require('querystring');

// const currentTime = new Date('2020-09-10T19:00:00+08:00');

/* GET home page. */
router.get("/getcities", function (req, res, next) {
  db.getCities({ name: "北京", pos: { $ne: "" } }, "time", (data) => {
    // console.log(data[0].time);
    db.getCities(
      { time: data[0].time },
      "_id name aqi level pos time",
      (data) => {
        res.json(data);
      }
    );
  });
});

router.get("/getcitydetail", function (req, res, next) {
  db.getCityDetail({ city: "北京", pos: { $ne: "" } }, "time", (data) => {
    db.getCityDetail(
      { time: data[0].time },
      "city station aqi level pm25 pm10 mainPollution pos time",
      (data) => {
        res.json(data);
      }
    );
  });
});

router.get("/getcitiestopten", function (req, res, next) {
  db.getCities({ name: "北京", pos: { $ne: "" } }, "time", (data) => {
    // console.log(data[0].time);
    db.getCitiesTopTen(
      { time: data[0].time, pos: { $ne: 0 }, aqi: { $ne: 0 } },
      "name aqi level time",
      (data) => {
        res.json(data);
      }
    );
  });
});

router.get("/getcitiesbottomten", function (req, res, next) {
  db.getCities({ name: "北京", pos: { $ne: "" } }, "time", (data) => {
    // console.log(data[0].time);
    db.getCitiesBottomTen(
      { time: data[0].time },
      "name aqi level time",
      (data) => {
        res.json(data);
      }
    );
  });
});

router.get("/get-cities-history/:id", function (req, res, next) {
  db.getCities({ _id: req.params.id }, "name", (data) => {
    db.getCitiesHistory(
      { name: data[0].name, aqi: { $ne: 0 } },
      "name aqi level time",
      (data) => {
        res.json(data);
      }
    );
  });
});

// 登录验证
// router.post('/login', function (req, res) {
//   db.getUser(req.body, function (data) {
//     console.log(data)
//     res.send('登录成功!')
//   })
// })
module.exports = router;